package com.fsl.springbootdesignpattern.n_prototype;

import java.util.ArrayList;
import java.util.List;

public class PrototypeTest {

    public static void main(String[] args) {

        //属性都是基本类型或者Sting类型
        User user = new User();
        user.setAge(18);
        user.setName("Tom");
        try {
            //直接克隆，不需要走构造方法，效率更高,克隆出来的对象是一个全新的对象
            //能够直接拷贝其内容的就是：8大基本数据类型和String
            User userCopy = (User)user.clone();
            System.out.println(userCopy.getAge());
            System.out.println(userCopy.getName());
            //false
            System.out.println(user == userCopy);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }


        System.out.println("--------------------------------------");


        //属性包含非基本数据类型ArrayList
        Student student = new Student();
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        student.setList(list);
        student.setAge(19);
        student.setName("xiaohong");
        try {
            Student studentCopy = (Student)student.clone();
            System.out.println(studentCopy.getAge());
            System.out.println(studentCopy.getName());
            //给student1当中新增一个元素
            studentCopy.getList().add("d");
            //（没有重写clone方法之前）这里简单来思考来说是结果是3，但是现实显示是4，说明是浅拷贝，student和studentCopy指向的是同一个ArrayList对象
            System.out.println(studentCopy.getList().size());
            //false
            System.out.println(student == studentCopy);
            //true
            System.out.println(student.getList() == studentCopy.getList());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }


    }
}
